﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiserBlogData.Entities;
using MiserBlogMVC.Models;

namespace MiserBlogMVC.Controllers
{
    [MiserBlogMVC.Filter.AuthorUserVIPFilter]
    public class DiscussionController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }


        [ValidateInput(false)]
        public ActionResult Create(int? id)
        {
            if (id.HasValue)
            {
                MiserDB.DataHelp.QueryHelper data = new MiserDB.DataHelp.QueryHelper();
                Discussion discussion = data.GetData<Discussion>(id.Value);
                return View(discussion);
            }
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Discussion discussion, int? id)
        {
            UserVIPIdentity vipIdentity = (UserVIPIdentity)HttpContext.User.Identity;
            UserVIP vip = (UserVIP)vipIdentity.Person;
            discussion.User = vip;
            if (id.HasValue)
            {

                using (MiserDB.DataHelp.ActionTranHelper data = new MiserDB.DataHelp.ActionTranHelper())
                {
                    discussion.CDate = discussion.CDate;
                    discussion.UDate = DateTime.Now;
                    data.SaveOrUpdate<Discussion>(discussion);
                    return RedirectToRoute(new { action = "Index", controller = "ManageArticle" });
                }
            }
            else
            {
                using (MiserDB.DataHelp.ActionTranHelper data = new MiserDB.DataHelp.ActionTranHelper())
                {
                    discussion.CDate = DateTime.Now;
                    int newId = (int)data.Save<Discussion>(discussion);
                    discussion.GroupID = newId;
                    data.SaveOrUpdate<Discussion>(discussion);
                    return RedirectToRoute(new { action = "Index", controller = "ManageArticle" });
                }
            }
        }
    }
}
